package org.proddevplm.business.administration;

import java.util.List;
import java.util.Map;

import org.proddevplm.business.administration.constants.AdminConstants;
import org.proddevplm.business.api.AController;
import org.proddevplm.constants.ControllerConstants;
import org.proddevplm.dao.data.Roles;
import org.proddevplm.dao.data.administration.AccessLevel;
import org.proddevplm.dao.data.administration.Statistics;
import org.proddevplm.dao.data.designForX.DesignForX;
import org.proddevplm.dao.data.designForX.DfXGuidelines;

public class AdminController extends AController{

	@Override
	public Object execute(int command, Object data) {
		
		Map<String, Object> adminMap = (Map<String, Object>) data;
		int projectId = (Integer) getSession().getAttribute("projectId");
		
		switch(command){
		
		case AdminConstants.SET_ACCESS_LEVEL:
			int accessLevel = Integer.valueOf((String)adminMap.get("accessLevel"));
			int roleId = Integer.valueOf((String)adminMap.get("roleId"));
			getDaoManager().getAdminManager().setAccessLevels(projectId, roleId, accessLevel);
			
			Boolean accessLevelsSet = true;
			putOnSession("accessLevelsSet", accessLevelsSet);

            break;
            
		case AdminConstants.EDIT_ACCESS_LEVELS:
			int newAccessLevel = Integer.valueOf((String)adminMap.get("newLevel"));
			int accessLevelId = Integer.valueOf((String)adminMap.get("accessLevelId"));
			
			AccessLevel currentAL = getDaoManager().getAdminManager().getAccessLevelById(accessLevelId);
			getDaoManager().getAdminManager().editAccessLevels(newAccessLevel, currentAL);
			break;
            
		case AdminConstants.ADMIN_FWD:
			Boolean completed = (Boolean) getSession().getAttribute("accessLevelsSet");
			if((completed!=null)&& (completed)){
				getDaoManager().getAdminManager().setStatistics(projectId, "Access levels", completed);
			}
			
			putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
            putOnSession("currentTab", "tab11");
            break;
            
		case AdminConstants.STATISTICS:
			Statistics statResAccessLevels = getDaoManager().getAdminManager().getStatisticsByProjectandParameter(projectId, "Access levels");
			putOnSession("statResAccessLevels", statResAccessLevels);
			
			List<AccessLevel> allAccessLevelsByProject = getDaoManager().getAdminManager().getAccessLevelsByProjectId(projectId);
			putOnSession("allAccessLevelsByProject", allAccessLevelsByProject);
			
			putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
            putOnSession("currentTab", "tab11");
            break;
            
		case AdminConstants.ROLES:
			List<Roles> allRolesInDB = getDaoManager().getRoleManager().getAllRoles();
            putOnSession("allRolesInDB", allRolesInDB);
            
            putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
            putOnSession("currentTab", "tab11");
            break;
            
		case AdminConstants.ADD_ROLES:
			try{
				String[] array = (String[])adminMap.get("rolesArray");
				if(array.length!=0){	
					for(int i=0; i<array.length; i++){
						if(getDaoManager().getRoleManager().getRoleByName(array[i])==null){
							Roles role = new Roles();
							role.setRole(array[i]);
							getDaoManager().getRoleManager().addRole(role);
						}
					}
				}
			}catch(Exception e){
				e.printStackTrace();
			}
			
			putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
            putOnSession("currentTab", "tab11");
            break;
            
		case AdminConstants.DFX:
			List<DesignForX> allDFXInDB = getDaoManager().getDataAccessManager().getAllDfX();
			putOnSession("allDFXInDB", allDFXInDB);
            
			putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
            putOnSession("currentTab", "tab11");
            break;
            
		case AdminConstants.ADD_DFX:
			try{
				DesignForX newDfx = new DesignForX();
				newDfx.setDfxName(adminMap.get("newDfX").toString());
				newDfx.setInfo(adminMap.get("dfXDetailedInfo").toString());
				
				getDaoManager().getToolsManager().addDFX(newDfx);
				
			}catch(Exception e){
				e.printStackTrace();
			}
			
			putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
            putOnSession("currentTab", "tab11");
            break;
		
		case AdminConstants.EDIT_DFX:
			try{
				DesignForX dfx = getDaoManager().getDataAccessManager().getDfXById(Integer.valueOf(adminMap.get("dfxId").toString()));
				dfx.setDfxName(adminMap.get("dfxName").toString());
				dfx.setInfo(adminMap.get("dfxDetailedInfo").toString());
				
				getDaoManager().getToolsManager().updateDFX(dfx);
				
			}catch(Exception e){
				e.printStackTrace();
			}
			
			putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
            putOnSession("currentTab", "tab11");
            break;
            
		case AdminConstants.GET_DFX:
			DesignForX selectedDfx = getDaoManager().getDataAccessManager().getDfXById(Integer.valueOf(adminMap.get("dfxId").toString()));
			putOnSession("selectedDfx", selectedDfx);
			
			putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
            putOnSession("currentTab", "tab11");
            break;
            
		case AdminConstants.GET_GUIDELINES:
			List<DfXGuidelines> guidelinesByDFXConcept = getDaoManager().getDataAccessManager().getGuidelinesByDfxId(Integer.valueOf(adminMap.get("dfxId").toString()));
			putOnSession("guidelinesByDFXConcept", guidelinesByDFXConcept);
			
			putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
            putOnSession("currentTab", "tab11");
            break;
            
		case AdminConstants.GET_SPECFIC_GUIDELINE:
			DfXGuidelines specificGuideline = getDaoManager().getDataAccessManager().getSpecificGuidelineById(Integer.valueOf(adminMap.get("dfxGId").toString()));
			putOnSession("specificGuideline", specificGuideline);
			
			putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
            putOnSession("currentTab", "tab11");
            break;
            
		case AdminConstants.UPDATE_GUIDELINE:
			DfXGuidelines guideline = getDaoManager().getDataAccessManager().getSpecificGuidelineById(Integer.valueOf(adminMap.get("dfxGId").toString()));
			guideline.setInfo(adminMap.get("guidelineDetails").toString());
			getDaoManager().getToolsManager().updateDfXGuideline(guideline);
			
			putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
            putOnSession("currentTab", "tab11");
            break;
            
		case AdminConstants.ADD_GUIDELINE:
			try{
				DesignForX dfx = getDaoManager().getDataAccessManager().getDfXById(Integer.valueOf(adminMap.get("dfxId").toString()));
			
				DfXGuidelines dfxg = new DfXGuidelines();
				dfxg.setGuideline(adminMap.get("guidelineName").toString());
				dfxg.setInfo(adminMap.get("guidelineDetails").toString());
				dfxg.setDfx(dfx);
				
				getDaoManager().getToolsManager().addDfXGuideline(dfxg);
				
			}catch(Exception e){
				e.printStackTrace();
			}
			putOnState(ControllerConstants.NEXT_PAGE, "content/project/projectPage.jsp");
            putOnSession("currentTab", "tab11");
            break;
            
		}
		
		return null;
	}

}